<?php
/**
* @version		$Id: helper.php 2010-07-07 vinaora $
* @package		VINAORA MP3 PLAYER
* @copyright	Copyright (C) 2010 VINAORA. All rights reserved.
* @license		GNU/GPL
* @website		http://vinaora.com
* @email		admin@vinaora.com
* 
*/

// no direct accessd
defined( '_JEXEC' ) or die( 'Restricted access' );

require_once dirname(__FILE__).DS."helper".DS."vinaora_item.php";
require_once dirname(__FILE__).DS."helper".DS."vinaora_param.php";


class modVinaoraMP3PlayerHelper
{
	/*
	 * Get content of the config file
	 */
	function getConfig($path, $name){

		$path = JPath::clean($path);
		$path = $path.DS.$name;

		if (file_exists($path)){
			$xml = &JFactory::getXMLParser( 'simple' );
			$xml->loadFile($path);
			
			//TODO File exits but not valid XML
			return $xml;
		}
		else{
			//TODO File not exits
		}

		return NULL;

	}
	
	/*
	 * Create the config file
	 */
	function createConfig($path, $name, $xml){

		jimport('joomla.filesystem.file');
		$path = JPath::clean($path);

		if (is_writeable($path)){
			if ( JFile::write($path.DS.$name, $xml) ) return true;
			else{
				// TODO: Write file error
			}
		}
		else{
			// TODO: Folder is not writeable
		}

		return false;

	}
	
	/*
	 * Create content of the config file
	 */
	function createXML($params){
		// Create Element - <playlist>
		$node = new JSimpleXMLElement('playlist');
		
		// Create Track List
		$node = &modVinaoraMP3PlayerHelper::createTrackList($node, $params);
		
		$xml = '<?xml version="1.0" encoding="utf-8"?>';
		$xml .= $node->toString(true);
		
		$xml = str_replace('tracklist>', 'trackList>', $xml);

		return $xml;
	}
	
	/*
	 * Create TrackList
	 */
	function &createTrackList(&$node, $params){
		// Create Element - <playlist>.<trackList>
		$nodeL1	= &$node->addChild('trackList');
		$items	= explode("\n", $params->get('item_location'));
		for ($i=1; $i<=count($items); $i++){
			$nodeL1 = &modVinaoraMP3PlayerHelper::createItem($nodeL1, $params, $i);
		}
		return $node;
	}
	
	/*
	 * Create a Item Element
	 */
	function &createItem(&$node, $params, $position=1){
		// Create Element - <playlist>.<trackList>.<track>
		$nodeL1 = &$node->addChild('track');
		
		// Create Element - <playlist>.<trackList>.<track>.<location>
		$param	= $params->get('item_location');
		$param	= modVinaoraMP3PlayerParam::getParam($param, $position, "\n");
		$nodeL2	= &$nodeL1->addChild('location');
		if (!empty($param)){
			$nodeL2->setData($param);
		}
		
		// Create Element - <playlist>.<trackList>.<track>.<title>
		$param	= $params->get('item_title');
		$param	= modVinaoraMP3PlayerParam::getParam($param, $position, "\n");
		$nodeL2	= &$nodeL1->addChild('title');
		if (!empty($param)){
			$nodeL2->setData($param);
		}
		
		// Create Element - <playlist>.<trackList>.<track>.<creator>
		$param	= $params->get('item_creator');
		$param	= modVinaoraMP3PlayerParam::getParam($param, $position, "\n");
		$nodeL2	= &$nodeL1->addChild('creator');
		if (!empty($param)){
			$nodeL2->setData($param);
		}
		
		// Create Element - <playlist>.<trackList>.<track>.<image>
		// $param	= $params->get('item_image');
		// $param	= modVinaoraMP3PlayerParam::getParam($param, $position, "\n");
		// $nodeL2	= &$nodeL1->addChild('image');
		// $nodeL2->setData($param);
		
		return $node;
	}
	
	/*
	 * Get List of Items from a Folder
	 */
	function getItems($folder, $fullpath=false){

		$root		= JPATH_BASE.DS.'images'.DS.'stories';
		$default	= JPATH_BASE.DS.'media'.DS.'mod_vinaora_mp3player'.DS.'mp3'.DS.'default';
		
		$filter 	= '\.mp3$';
		$exclude	= array('index.html', '.htaccess');
		
		$items		= modVinaoraMP3PlayerItem::getItems($folder, $root, $default, $filter, $exclude, $fullpath);
		
		return $items;

	}
	
	/*
	 * Get Item Path
	 */
	function getItemPath($folder){
		
		$root		= 'images/stories/';
		$default	= VINAORA_MP3PLAYER_MEDIA_URL.'mp3/default';
		
		$path		= modVinaoraMP3PlayerItem::getItemPath($folder, $root, $default);
		
		return $path;
		
	}
	
	/*
	 * Get List of Item URLs
	 */
	function getItemPaths($folder){
		$path 	= modVinaoraMP3PlayerHelper::getItemPath($folder);
		$items	= modVinaoraMP3PlayerHelper::getItems($folder);
		
		if (count($items)){
			foreach($items as $key=>$value){
				$items[$key] = $path.'/'.$value;
			}
		}

		return $items;
	}
}
